home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / COUPON.TST / ADDFRAC.C < prev    next >
C/C++ Source or Header  |  1995-10-16  |  1KB  |  49 lines

  1. /* ============ */
  2. /* AddFrac.c    */
  3. /* ============ */
  4. #include <defcodes.h>
  5.  
  6. /* ------------------- */
  7. /* FUNCTION PROTOTYPES */
  8. /* ------------------- */
  9. # undef F
  10. # if defined(__STDC__) || defined(__PROTO__)
  11. #    define    F( P )    P
  12. # else
  13. #    define    F( P )    ()
  14. # endif
  15.  
  16. /* INDENT OFF */
  17. extern    long    double GetGCD F((long double, long double));
  18.  
  19. # undef F
  20. /* INDENT ON */
  21.  
  22. #define    REDUCE_FRACTION(N3, D3)        \
  23.     {                    \
  24.     LDBL    Gcd;            \
  25.     Gcd = GetGCD(N3, D3);        \
  26.     if (Gcd == 1)            \
  27.     {                \
  28.         /* Do Nothing. */        \
  29.     }                \
  30.     else                \
  31.     {                \
  32.         N3 /= Gcd;            \
  33.         D3 /= Gcd;            \
  34.     }                \
  35.     }
  36.  
  37. /* ==================================================================== */
  38. /* AddFrac - Adds two fraction given as Numer & Denom, stores new ones    */
  39. /* ==================================================================== */
  40. void
  41. AddFrac(long double N1, long double D1, long double N2, long double D2,
  42.     long double *N3, long double *D3)
  43. {
  44.     *N3 = N2 * D1 + N1 * D2;
  45.     *D3 = D1 * D2;
  46.  
  47.     REDUCE_FRACTION(*N3, *D3);
  48. }
  49.